﻿using System;
using System.Linq;
using System.Collections.Generic;

namespace HashFoo.Rest.Common
{
    /// <summary>
    /// Function based serializer -- set the underlying <see cref="Reader"/>,
    /// <see cref="Writer"/>,<see cref="AcceptMimeTypes"/> and <see cref="ContentMimeType"/>.
    /// </summary>
    public class FuncResourceSerializer : IResourceSerializer
    {
        /// <summary>
        /// The backing reader functioin of the serailizer.
        /// </summary>
        public Func<string, Type, object> Reader { get; set; }

        /// <summary>
        /// The backing writer function of the serializer.
        /// </summary>
        public Func<object, string> Writer { get; set; }

        /// <summary>
        /// Reads an object from a string.
        /// </summary>
        /// <typeparam name="T">The type of object to be returned</typeparam>
        /// <param name="data">The string data to deserialize into the object</param>
        /// <returns>The deserialized object.</returns>
        public T Read<T>(string data)
        {
            return (T)Reader(data, typeof(T));
        }

        public object Read(string data, Type type)
        {
            return Reader(data, type);
        }

        /// <summary>
        /// Write an object to a serialized string.
        /// </summary>
        /// <typeparam name="T">The type of object to serialize.</typeparam>
        /// <param name="data">The data to serialize.</param>
        /// <returns>The serialized data string.</returns>
        public string Write<T>(T data)
        {
            return Writer(data);
        }

        /// <summary>
        /// The MIME types this serializer will accept
        /// </summary>
        public string[] AcceptMimeTypes { get; set; }

        /// <summary>
        /// The MIME type to which content is serialized.
        /// </summary>
        public string ContentMimeType { get; set; }
    }
}